package com.chromatic.competition.view.notifications;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chromatic.competition.R;

import java.util.ArrayList;
import java.util.List;

public class FriendsAddActivity extends AppCompatActivity {

    private EditText searchEditText;
    private LinearLayout resultsLayout;
    private List<FriendSearched> friendList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends_add);

        searchEditText = findViewById(R.id.search);
        resultsLayout = findViewById(R.id.resultsLayout);

        ImageView backButton = findViewById(R.id.backButton);
        backButton.setOnClickListener(v -> {
            onBackPressed(); // 调用系统返回操作
        });

        friendList = new ArrayList<>();

        // 示例数据，用于显示搜索结果
        friendList.add(new FriendSearched("1", "John Doe", "John", "1234567890", "Harvard University"));
        friendList.add(new FriendSearched("2", "Jane Smith", "Jane", "9876543210", "Stanford University"));

        findViewById(R.id.searchButton).setOnClickListener(v -> {
            String searchText = searchEditText.getText().toString().trim();
            searchFriends(searchText);
        });
    }

    private void searchFriends(String searchText) {
        resultsLayout.removeAllViews(); // 清空搜索结果

        for (FriendSearched friend : friendList) {
            // 判断搜索条件是否匹配昵称、真实姓名或手机号
            if (friend.getNickname().toLowerCase().contains(searchText.toLowerCase()) ||
                    friend.getRealName().toLowerCase().contains(searchText.toLowerCase()) ||
                    friend.getPhoneNumber().contains(searchText)) {
                @SuppressLint("InflateParams") View friendView = getLayoutInflater().inflate(R.layout.add_friends_item, null);
                TextView friendNameTextView = friendView.findViewById(R.id.friendNameTextView);
                TextView friendSchoolTextView = friendView.findViewById(R.id.friendSchoolTextView);
                ImageView addButton = friendView.findViewById(R.id.addButton);

                friendNameTextView.setText(friend.getNickname());
                friendSchoolTextView.setText(friend.getSchool());

                // 如果好友已添加，则隐藏加号
                if (friend.isAdded()) {
                    addButton.setVisibility(View.GONE);
                } else {
                    addButton.setVisibility(View.VISIBLE);
                    addButton.setOnClickListener(v -> {
                        // 处理添加好友的操作
                        friend.setAdded(true);
                        addButton.setVisibility(View.GONE);
                        Toast.makeText(FriendsAddActivity.this, "好友已添加", Toast.LENGTH_SHORT).show();
                    });
                }

                resultsLayout.addView(friendView); // 将好友视图添加到结果布局中
            }
        }
    }

    // FriendSearched类作为FriendsAddActivity的内部类
    static class FriendSearched {
        private final String id;
        private final String realName;
        private final String nickname;
        private final String phoneNumber;
        private final String school;
        private boolean added;
        public FriendSearched(String id, String realName, String nickname, String phoneNumber, String school) {
            this.id = id;
            this.realName = realName;
            this.nickname = nickname;
            this.phoneNumber = phoneNumber;
            this.school = school;
            this.added = false;
        }
        public String getId() {
            return id;
        }
        public String getRealName() {
            return realName;
        }
        public String getNickname() {
            return nickname;
        }
        public String getPhoneNumber() {
            return phoneNumber;
        }
        public String getSchool() {
            return school;
        }
        public boolean isAdded() {
            return added;
        }
        public void setAdded(boolean added) {
            this.added = added;
        }
    }
}